package mysql

import (
	"e_mall_server/models"
	"go.uber.org/zap"
)

/**
 * @Author: zhaobin
 * @Description 对表goods_cate进行相关操作
 * @Date: 2022-05-09 11:46
 */

// GetGoodsCateList 获取商品分类列表接口
func GetGoodsCateList(query map[string]interface{}) ([]models.GoodsCate, error) {
	var goodsCateList []models.GoodsCate
	if err := db.Where(query).Preload("GoodsCateItems").Find(&goodsCateList).Error; err != nil {
		return nil, err
	}
	return goodsCateList, nil
}

// AddGoodsCate 添加商品分类信息
func AddGoodsCate(goodsCate *models.GoodsCate) error {
	zap.L().Debug("mysql.AddGoodsCate()", zap.Any("商品分类信息", goodsCate))
	// 进行数据库添加操作
	return db.Create(goodsCate).Error
}

// UpdateGoodsCate 修改商品分类信息
func UpdateGoodsCate(goodsCate *models.GoodsCate) int64 {
	// 进行数据库修改操作
	value := db.Debug().Model(&goodsCate).Updates(&goodsCate)
	return value.RowsAffected
}

// DeleteGoodsCateById 通过商品分类id删除商品分类数据
func DeleteGoodsCateById(id int64) int64 {
	return db.Delete(&models.GoodsCate{Id: id}).RowsAffected
}

// GetGoodsCateListByParentId 查看父模块为GoodsCatePId的子模块
func GetGoodsCateListByParentId(parentId int64) []models.GoodsCate {
	var goodsCateList []models.GoodsCate
	db.Where("parent_id = ?", parentId).Find(&goodsCateList)
	return goodsCateList
}

// GetGoodsCateDetailById 通过商品分类id获取商品分类详细信息
func GetGoodsCateDetailById(id int64) (goodsCate models.GoodsCate, err error) {
	goodsCate.Id = id
	err = db.Where("id = ?", id).Find(&goodsCate).Error
	return
}
